package com.ywz.framework.mybatisplus;

import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.provider.DynamicDataSourceProvider;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DruidDynamicDataSourceConfiguration;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceAopConfiguration;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceAssistConfiguration;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceCreatorAutoConfiguration;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.ywz.common.SymmetricAlgorithmUtils;
import com.ywz.framework.property.MysqlProperties;
import com.zaxxer.hikari.HikariDataSource;
import lombok.AllArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 类描述-> MyBatisPlus配置
 *
 * @Author: ywz
 * @Date: 2024/09/29
 */
@Configuration
@AllArgsConstructor
@Import({DruidDynamicDataSourceConfiguration.class, DynamicDataSourceCreatorAutoConfiguration.class, DynamicDataSourceAopConfiguration.class, DynamicDataSourceAssistConfiguration.class})
public class MybatisPlusConfig {

    private final MysqlProperties mysqlProperties;

    /**
     * MyBatisPlus拦截器
     */
    @Bean
    public MybatisPlusInterceptor paginationInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

    @Bean
    public DataSource getDynamicDataSource() {
        List<DynamicDataSourceProvider> providers = new ArrayList<>();
        DynamicDataSourceProvider provider = () -> {
            Map<String, DataSource> map = new HashMap<>();
            HikariDataSource dataSource = new HikariDataSource();
            dataSource.setJdbcUrl(SymmetricAlgorithmUtils.decrypt(mysqlProperties.url()) + mysqlProperties.urlSuffix());
            dataSource.setUsername(SymmetricAlgorithmUtils.decrypt(mysqlProperties.username()));
            dataSource.setPassword(SymmetricAlgorithmUtils.decrypt(mysqlProperties.password()));
            dataSource.setDriverClassName(mysqlProperties.driverClassName());
            map.put("master", dataSource);
            return map;
        };
        providers.add(provider);
        return new DynamicRoutingDataSource(providers);
    }

}
